﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using pcsse.App_Code.entity;

namespace pcsse.MasterPages
{
    public partial class activity : System.Web.UI.Page
    {
        public const String UpAttach = "UpAttach/";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists(Server.MapPath(UpAttach)))
                {
                    Directory.CreateDirectory(Server.MapPath(UpAttach));
                }
                errLb.Text = "";
                getInfoByID();
            }
            catch (System.Exception ex)
            {
                errLb.Text = ex.ToString();
            }
        }

        private void getInfoByID()
        {
            if (Session["ID"] != null)
            {
                int id = Convert.ToInt32(Session["ID"]);
                person_info pi = new person_info();
                String number = pi.GetNumberByID(id);
                String branchname = pi.GetBranchNameByID(id);
                String branchid = pi.GetBranchIDByID(id).ToString();
                Label bLb = (Label)activityDv.FindControl("branchLb");
                if (bLb != null)
                {
                    bLb.Text = branchname;
                }
                Label nLb = (Label)activityDv.FindControl("numberLb");
                if (nLb != null)
                {
                    nLb.Text = number;
                }
                Label bidLb = (Label)activityDv.FindControl("branchIDLb");
                if (bidLb != null)
                {
                    bidLb.Text = branchid;
                }
            }
            else
            {
                Response.Redirect("~/default.aspx");
            }
        }

        protected void upLoadBt_Click(object sender, EventArgs e)
        {
            try
            {
                FileUpload attachFu = (FileUpload)activityDv.FindControl( "attachFu" );
                if(attachFu!=null)
                {
                    if ( attachFu.HasFile )
                    {
                        string filename = System.DateTime.Now.ToFileTimeUtc() + attachFu.FileName;
                        attachFu.SaveAs( Server.MapPath( UpAttach ) + filename );
                        TextBox tb = ( (TextBox)activityDv.FindControl( "upFileTb" ) );
                        tb.Text = filename;
                    }
                    else
                    {
                        errLb.Text = "请选择文件";
                    }
                }else{
                    errLb.Text = "找不到文件上传控件，请与管理员联系";
                }
            }
            catch (System.Exception ex)
            {
                errLb.Text = ex.ToString();
            }
        }

        protected void activityDv_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                Exception ie = e.Exception.InnerException;
                if (ie is ArgumentException)
                {
                    string argu = ((ArgumentException)ie).ParamName;
                    errLb.Text = "参数[";
                    errLb.Text += argu;
                    errLb.Text += "]不合法!";
                }
                else if (ie is NoNullAllowedException)
                {
                    errLb.Text = "一个或多个字段为空!";
                }
                else if (ie is System.Data.Common.DbException)
                {
                    errLb.Text = "数据库异常，请稍后再试";
                }
                else
                {
                    errLb.Text = "未知错误，请稍后再试";
                }
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
            else
            {
                Response.Redirect( "~/activity/manageactivity.aspx" );
            }
        }

        protected void activityDv_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {

        }

        protected void activityDv_DataBound(object sender, EventArgs e)
        {
            getInfoByID();
        }
    }
}
